
#include "skyfall.h"
#include "resourcemanager.h"
#include "gamemanager.h"
#include "frameanimation.h"
#include "CCSkeletalSprite.h"
#include "gamedatamanager.h"
#include "army.h"
#include "unit.h"

static const float SKY_FALL_TIME = 0.5f;

#define __super         Bullet

SkyFallBullet::SkyFallBullet(
		Army *targetArmy,
		EquipmentAttribute *attr,
		GameScene *scene):
__super(attr)
, _targetArmy(targetArmy)
, _gameNodeContainer(scene)
, _bolt(nullptr) {

}

SkyFallBullet::~SkyFallBullet() {
}

void SkyFallBullet::init() {
}

bool SkyFallBullet::update(float ticks) {
	bool ret = true;

	updateBolt(ticks);

	if (isHitTarget()) {
		_bolt->removeFromParent();

		createHitEffect();
		applyDamageToTargets();

		ret = false;
	}

	return ret;
}

void SkyFallBullet::applyDamageToTargets() {
	int squadIndex = _targetArmy->frontLine / LINES_PER_SQUAD;
	for (int i = _targetArmy->frontLine; i < (squadIndex + 1) * LINES_PER_SQUAD; i++) {
		if(i > _targetArmy->frontLine && bulletAttribute->attackType == AttackType::cast_squad_front_line) {
			break;
		}

		for(int j = 0; j < ROWS_PER_LINE; j++) {
			Unit* unit = _targetArmy->soldiers[i][j];
			if(unit && unit->isAlive()) {
				unit->applyBullet(this);
			}
		}
	}
}
